import { _decorator, Collider2D, Component, Contact2DType, Node } from 'cc';
import { GameManager, GameState } from '../GameManager';
const { ccclass, property } = _decorator;

@ccclass('Bullet_Player_1')
export class Bullet_Player_1 extends Component {
    
    @property
    bullet_1_Speed:number=1200;
    
    collider: Collider2D = null;

    hited:boolean=false;

    start() {
        this.collider=this.node.getComponent(Collider2D);
        this.collider.on(Contact2DType.BEGIN_CONTACT,()=>{
            this.collider.off(Contact2DType.BEGIN_CONTACT);
            if(this.node){
                // console.log("b1");
                this.hited=true;
            }
        })
    }

    update(deltaTime: number) {
        if (GameManager.getInstance().gameState == GameState.START){
                
            if(this.node){
                    
                const newPos=this.node.getPosition();

                newPos.y+=this.bullet_1_Speed*deltaTime;

                this.node.setPosition(newPos);

                if(this.node.position.y>=1000 && this.node){
                    this.node.destroy();
                }
            }
            if(this.hited){
                if(this.node){
                    this.node.destroy();
                }
            }
        }
        
    }
}


